﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.IDAL;
using RFIDStockManagement.DAL;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Model.InterFace;

namespace RFIDStockManagement.BLL
{
    public class InstockManager
    {

        public bool Save(InStockBill value, out string errMsg)
        {
           
            IInStockBill billAccess = DalFactory.CreateInstockbillAcess();
            IInventroyManage iventrory;// = DalFactory.CreateInventroyManage();

            Trans tran = new Trans();
            try
            {
                //保存入库单的数据
                if (!billAccess.InsertInStockBill(value, tran, out errMsg))
                {
                    tran.RollBack();
                    return false;
                }


                IInventory thisInventory = billAccess as IInventory;


                //这里处理及时库存
                if (thisInventory == null)
                {
                    tran.Commit();
                }
                else
                {
                    iventrory = DalFactory.CreateInventroyManage();

                    if (iventrory.ChangeInventroy(thisInventory.GetInventoryDatas(), tran, true, out errMsg))
                    {
                        tran.Commit();
                        return true;
                    }
                    else
                    {
                        tran.RollBack();
                        return false;
                    }
                }
                

            }
            catch (Exception ex)
            {
                tran.RollBack();
                errMsg = ex.Message;
                return false;
            }
            finally
            {
                tran.Dispose();
            }

            return true;

        }
    }
}
